<?php

namespace Modules\Manager\Http\Controllers\Admin;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Manager\Http\Requests\PermissionRequest;
use Spatie\Permission\Models\Permission;

class PermissionController extends Controller
{
    use ResponseData;
    public function list(Request $request)
    {
        $size = $request->input('size',10);
        $role = Permission::query()
            ->select('id','route','zh_name','name','module_name','sort','icon','guard_name','type','two_type')
            ->orderByDesc('sort')
            ->paginate($size);
        return $this->success($role);
    }
    public function create()
    {
        $role = config('permission.module');
        return $this->success($role);
    }
    public function store(PermissionRequest $request)
    {
        $data = $request->all();
        $data['guard_name'] = 'sanctum';
        $role = Permission::create($data);
        return $this->success($role);
    }

    public function edit(Request $request)
    {
        $data = Permission::query()
            ->select('id','route','zh_name','name','module_name','sort','icon','guard_name','type','two_type')
            ->find($request->id);
        $data->module = config('permission.module');
        return $this->success($data);
    }

    public function update(Request $request)
    {
        $data = $data = Permission::query()->find($request->id);
        $data->update($request->all());
        return $this->success('更新成功');
    }
    public function destory(Request $request)
    {
        $data = $data = Permission::query()->find($request->id);
        if($data){
            $data->delete();
            return $this->success('删除成功');
        }else{
            return $this->failed('不存在');
        }

    }
}
